package com.tcu.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcu.user.entity.Role;
import com.tcu.user.mapper.PermissionMapper;
import com.tcu.user.mapper.RoleMapper;
import com.tcu.user.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    private PermissionMapper permissionMapper;

    @Autowired
    public void setPermissionMapper(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }

    @Override
    @Transactional
    public boolean updatePermissionForRole(String roleId, List<String> permissionIds) {
        permissionMapper.deletePermissionIdsByRid(roleId);
        permissionIds.forEach(pid->{
            permissionMapper.addPermissionForRole(roleId,pid);
        });
        return true;
    }
}
